package real.tuhu;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class MaxValue {

    //有n个数，随机排列成一个最大的数，输出一个字符串，例:[3,10,24,25]，输出:"3252410"
    public String maxValue(int[] array) {
        List<String> list = Arrays.stream(array)
                .mapToObj(String::valueOf)
                .collect(Collectors.toList());
        list.sort((a, b) -> (b + a).compareTo(a + b));
        StringBuilder sb = new StringBuilder();
        for (String s : list) {
            sb.append(s);
        }
        // 去除前导0
        String result = sb.toString();
        return result.replaceFirst("^0+(?!$)", "");
    }
}
